<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>
</head>
<body>
<?php
include("../connect/connect.php");

	$Query = "SELECT * FROM product,category WHERE product.category_id = category.category_id ";

$sortcost = $_GET["sortcost"];
$sortqty = $_GET["sortqty"];
$sortcode = $_GET["sortcode"];
$sortname = $_GET["sortname"];
$sortrop = $_GET["sortrop"];
$sortcate = $_GET["sortcate"];
$sortctname = $_GET["sortctname"];

if(isset($_GET["sortcate"])){

	if($_GET["sortcate"]=="0"){
	$Query .= "";
	} else {
	$Query .= " AND category.category_id = $sortcate ";
	}
}

	
if(isset($_GET["sortcode"])){
	$Query .= "ORDER BY code $sortcode";
}
	
if(isset($_GET["sortcost"])){
	$Query .= "ORDER BY price $sortcost";
}

if(isset($_GET["sortqty"])){
	$Query .= "ORDER BY quantity $sortqty";
}

if(isset($_GET["sortname"])){
	$Query .= "ORDER BY name $sortname";
}

if(isset($_GET["sortrop"])){
	$Query .= "ORDER BY rop $sortrop";
}

if(isset($_GET["sortctname"])){
	$Query .= "ORDER BY category_name $sortctname";
}

$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>รายงานข้อมูลสินค้าคงเหลือ</h1>
	</div>
  <div class="content"><div class="buttons" align="right"><b>แสดงข้อมูลตามหมวดหมู่สินค้า : </b>
      <select name="category_id" id="category_id" onchange="location.href=this.options[this.selectedIndex].value">

			<option <?php if($sortcate=="0") {echo "selected";} ?> value="?sortcate=0<?php if(isset($sortcost)){echo "&sortcost=$sortcost";} ?><?php if(isset($sortqty)){echo "&sortqty=$sortqty";} ?><?php if(isset($sortcode)){echo "&sortcode=$sortcode";} ?><?php if(isset($sortname)){echo "&sortname=$sortname";} ?><?php if(isset($sortrop)){echo "&sortrop=$sortrop";} ?><?php if(isset($sortctname)){echo "&sortctname=$sortctname";} ?>">ทุกหมวดหมู่</option>
              <?php $Querycate = "SELECT * FROM category ORDER BY category_id";
			$objQuerycate = mysql_query($Querycate);
			while($row1 = mysql_fetch_array($objQuerycate))
			{
		?>
              <option <?php if($sortcate==$row1["category_id"]) {echo "selected";} ?> value="?sortcate=<?php echo $row1["category_id"];?><?php if(isset($sortcost)){echo "&sortcost=$sortcost";} ?><?php if(isset($sortqty)){echo "&sortqty=$sortqty";} ?><?php if(isset($sortcode)){echo "&sortcode=$sortcode";} ?><?php if(isset($sortname)){echo "&sortname=$sortname";} ?><?php if(isset($sortrop)){echo "&sortrop=$sortrop";} ?>">
                <?php echo $row1["category_name"];?>
              </option>
              <?php }
			?>
      </select></div><br />
    <table class="list">
        <thead>
          <tr>
            <td width="109" style="text-align: center;"><span class="center"><?php if($sortcode=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=desc">รหัสสินค้า <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortcode=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=asc">รหัสสินค้า <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcode=asc">รหัสสินค้า</a>
			<?php } ?></span></td>
            <td width="367" class="center"><?php if($sortname=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=desc">ชื่อสินค้า <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortname=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=asc">ชื่อสินค้า <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortname=asc">ชื่อสินค้า</a>
			<?php } ?></td>
            <td width="129" class="center"><?php if($sortctname=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=desc">หมวดสินค้า <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortctname=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=asc">หมวดสินค้า <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortctname=asc">หมวดสินค้า</a>
			<?php } ?></td>
            <td width="124" class="center"><?php if($sortcost=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=desc">ราคา <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortcost=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=asc">ราคา <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortcost=asc">ราคา</a>
			<?php } ?></td>
            <td width="132" class="center"><?php if($sortqty=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=desc">จำนวนสินค้าคงเหลือ <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortqty=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=asc">จำนวนสินค้าคงเหลือ <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortqty=asc">จำนวนสินค้าคงเหลือ</a>
			<?php } ?></td>
            <td width="114" class="center"><?php if($sortrop=='asc'){ ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortrop=desc">จุดสั่งซื้อ <img src="image/desc.png" width="13" height="13" border="0" /></a>
			<?php } else if($sortrop=='desc') { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortrop=asc">จุดสั่งซื้อ <img src="image/asc.png" width="13" height="13" border="0" /></a>
			<?php } else { ?>
			<a href="?<?php if(isset($sortcate)){echo "sortcate=$sortcate&";} ?>sortrop=asc">จุดสั่งซื้อ</a>
			<?php } ?></td>
          </tr>
        </thead>
        <tbody>
<?php
	$strnum=0;
	while($row = mysql_fetch_array($objQuery))
		{
		$strnum++;
?>
          <tr>
            <td style="text-align: center;"><?php echo $row["code"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_name"];?></td>
            <td class="right" style="padding-right:30px"><?php echo $row["price"];?></td>
            <td class="right" style="padding-right:30px"><?php echo $row["quantity"];?></td>
            <td class="center"><?php echo $row["rop"];?></td>
          </tr>
<?php
		}
		if($strnum==0) {
?>
        <tr>
          <td class="center" colspan="6">ไม่มีรายการ</td>
        </tr>
<?php
		}
?>
        </tbody>
    </table>
  </div>
</div>

</body>
</html>
